//-----------------------------------------------------------------------------
//  Copyright (C) 2005-2008 Thomas S. Ullrich 
//
//  This file is part of the Solitaire Mahjong project.
//
//  This file may be used under the terms of the GNU General Public License.
//  This project is free software; you can redistribute it and/or modify it
//  under the terms of the GNU General Public License.
//  
//  Author: Thomas S. Ullrich
//  Last update: June 1, 2007
//-----------------------------------------------------------------------------
#include <iostream>
#include <QLabel>
#include <QComboBox>
#include <QCheckBox>
#include <QApplication>
#include <QDir>
#include <QMessageBox>
#include <QColorDialog>
#include <QPixmap>
#include "smSettings.h"
#include "smPreferencesDialog.h"
 
using namespace std;
 
smPreferencesDialog::smPreferencesDialog(QWidget* parent, Qt::WindowFlags fl) : QDialog(parent, fl)
{
    ui.setupUi(this);
    init();
    move(smSettings::instance()->mPreferencesWindowPosition);
}

smPreferencesDialog::~smPreferencesDialog()
{ /* noop */ }


void smPreferencesDialog::init()
{
    ui.mTileSizeComboBox->setCurrentIndex(smSettings::instance()->mCurrentTileSize);
	QPalette palette;
    palette.setColor(ui.mPixmapLabel->backgroundRole(), smSettings::instance()->mBackgroundColor);
    ui.mPixmapLabel->setPalette(palette);
    ui.mBackgroundColorComboBox->setCurrentIndex(1); // 1 = current
    setTilePixmap(classic, static_cast<smTileSize>(smSettings::instance()->mCurrentTileSize));
    ui.mStatusBarInfoCheckBox->setChecked(smSettings::instance()->mShowTileNamesInStatusBar);
    ui.mSoundBeepCheckBox->setChecked(smSettings::instance()->mBeepWhenTileNotFree);
    ui.mRandomHintCheckBox->setChecked(smSettings::instance()->mRandomHint);
    ui.mShowNumberOfMovesCheckBox->setChecked(smSettings::instance()->mShowNumberOfPossibleMoves);
    setSizeGripEnabled(false);
    setModal(true);
}
 
void smPreferencesDialog::accept()
{
    smSettings::instance()->mCurrentTileSize = ui.mTileSizeComboBox->currentIndex();
    smSettings::instance()->mBackgroundColor = ui.mPixmapLabel->palette().color(ui.mPixmapLabel->backgroundRole());
    ui.mBackgroundColorComboBox->setCurrentIndex(1); // 1 = current
    smSettings::instance()->mShowTileNamesInStatusBar = ui.mStatusBarInfoCheckBox->isChecked();
    smSettings::instance()->mBeepWhenTileNotFree = ui.mSoundBeepCheckBox->isChecked();
    smSettings::instance()->mRandomHint = ui.mRandomHintCheckBox->isChecked();
    smSettings::instance()->mShowNumberOfPossibleMoves = ui.mShowNumberOfMovesCheckBox->isChecked();
     
    QDialog::accept();
}
 
void smPreferencesDialog::reject()
{
    init();
    QDialog::reject();
}
 
void smPreferencesDialog::tileSizeComboActivated(int index)
{
    setTilePixmap(classic, static_cast<smTileSize>(index));
    update();
}
 
void smPreferencesDialog::backgroundColorComboActivated(int index)
{
    QColor c;
	QPalette palette;
    switch(index) {
    case(0): // default
		palette.setColor(ui.mPixmapLabel->backgroundRole(), smSettings::instance()->mDefaultBackgroundColor);
		ui.mPixmapLabel->setPalette(palette);
        break;
    case(2): // new
        c = QColorDialog::getColor(smSettings::instance()->mBackgroundColor, this);
        if (c.isValid()) {
            smSettings::instance()->mBackgroundColor = c;
			palette.setColor(ui.mPixmapLabel->backgroundRole(), smSettings::instance()->mBackgroundColor);
			ui.mPixmapLabel->setPalette(palette);
		}
        break;
    default:
    case(1): // current
        break;
    }
    ui.mBackgroundColorComboBox->setCurrentIndex(1); // 1 = current
}
 
void smPreferencesDialog::setTilePixmap(smTileSetId tileId, smTileSize s)
{
    QString path = qApp->applicationDirPath() + "/tiles";
    QDir dir(path);
    if (!dir.exists()) {
        path = qApp->applicationDirPath() + "/../tiles";
        dir.setPath(path);
    }
#if defined(Q_OS_MAC)   
    if (!dir.exists()) {
        path = qApp->applicationDirPath() + "/../Resources/tiles";
        dir.setPath(path);
    }
#endif
    if (!dir.exists()) {
        QMessageBox::critical( 0, "Solitaire Mahjong",
                         "Cannot find the directory holding the tile sets (tiles).\n"
                         "Ceck your installation and reinstall if necessary.\n"
                         "Solitaire Mahjong will now exit.");
        exit(1);
    }
     
    //
    // Select the tile set.
    //
    path += "/";
    path += smTileSetDirectory[tileId];
    path += "/";
     
    //
    // Select the tile size.
    //
    path += smTileSetSubdirectory[s];
    path += "/";
     
    //
    // Load the tile
    //
    QString filepath;
     
    filepath = path; filepath += "Bamboo_1_normal.png";
    ui.mPixmapLabel->setPixmap(QPixmap(filepath));
}

void smPreferencesDialog::hideEvent(QHideEvent*)
{
    smSettings::instance()->mPreferencesWindowPosition = pos();   
}


void smPreferencesDialog::showEvent(QShowEvent*)
{
    move(smSettings::instance()->mPreferencesWindowPosition);
}
